package com.huawei.esdk.uc.sametime.popup.actions;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;

import com.huawei.esdk.uc.jna.Constants;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.models.Participant;
import com.huawei.esdk.uc.sametime.models.ParticipantList;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.util.PluginUtil;
import com.huawei.esdk.uc.sametime.widgets.EsdkUCConvWindow;
import com.ibm.collaboration.realtime.people.PeopleUtil;
import com.ibm.collaboration.realtime.people.Person;

/**
 * 选择多个联系人时候,右键菜单中的电话会议的控制类
 * @author y00175154
 */

public class EsdkConfMultiLiveNameActionDelegate extends
        AbstractObjectActionDelegate
{
    private static Logger logger = Logger
            .getLogger(EsdkConfMultiLiveNameActionDelegate.class);

    private Person[] persons;

    public void run(IAction action)
    {
        persons = getSelectedPersons();
        if (null != persons)
        {
            ParticipantList participantList = new ParticipantList();
            for (int i = 0; i < persons.length; i++)
            {
                if (PeopleUtil.isMe(persons[i]))
                {
                    continue;
                }
                Participant participant = new Participant();
                participant.setContactId(persons[i].getContactId());
                participant.setNumber(PluginUtil
                        .getUCAccountFromContactID(persons[i].getContactId()));
                participant.setCallNumType(Constants.AccountType.UC_ACCOUNT);
                participantList.addparticipants(participant);
            }

            // 如果当前没有通话,则可以发起电话会议,否则,给出提示
            if (ConstantsDefines.CallStatus.end == UCCallService
                    .getCallStatus())
            {
                try
                {
                    EsdkUCConvWindow window = EsdkUCConvWindow.getInstance();
                    if (participantList.getparticipants().size() >= 2)
                    {
                        window.setConfEnabled(true);
                        Person person = PeopleUtil.getLocalPerson();
                        Participant participant = new Participant();
                        participant.setContactId(person.getContactId());
                        participant.setNumber(PluginUtil
                                .getUCAccountFromContactID(person
                                        .getContactId()));
                        participant
                                .setCallNumType(Constants.AccountType.UC_ACCOUNT);
                        participantList.getparticipants().add(0, participant);
                    }
                    window.createUCConvWindow(participantList);
                    window.makeCall(false);
                }
                catch (Exception exception)
                {
                    logger.error(exception.getMessage());
                }
            }
            else
            {
                MessageDialog.openInformation(Display.getCurrent()
                        .getActiveShell(), Messages.TIPS, Messages.IS_CALLING);
            }

        }
    }

    public void selectionChanged(IAction action, ISelection selection)
    {
        boolean islogin = GlobalConfiguration.getInstance().isUCLogin();
        boolean isSametimeLogin = GlobalConfiguration.getInstance()
                .isSamtimeLogin();
        boolean isInCall = (CallStatus.end != UCCallService.getCallStatus());
        
        // uc或者sametime没登录,则不能使用创建会议功能
        if (islogin && isSametimeLogin && !isInCall)
        {
            boolean isSelectMutiPerson = ((IStructuredSelection) selection)
                    .size() >= 2;
            action.setEnabled(isSelectMutiPerson);
        }
        else
        {
            action.setEnabled(false);
        }
        super.selectionChanged(action, selection);
    }
}
